package com.wh.wisdomsite.xbox.utils.convert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 转换工具类
 * @Package com.wh.wisdomsite.xbox.utils.convert
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class ConvertTool {

    private static final Log logger = LogFactory.getLog(ConvertTool.class);

    private static final DecimalFormat simpleFormat = new DecimalFormat("####");

    /**
     * 将Object转换成BigDecimal
     * @param object
     * @return
     */
    public static BigDecimal ObjectToBigDecimal(Object object){
        return new BigDecimal(convert(object));
    }


    /**
     * 将BigDecimal转换成String
     * @param bigDecimal 转换值
     * @param scale 保留小数
     * @param roundingMode 保留模式
     * @return
     */
    public static String BigDecimalToString(BigDecimal bigDecimal, int scale, int roundingMode){
        if(null == bigDecimal){
            return "0.00";
        }
        if(0 == bigDecimal.doubleValue()){
            return "0.00";
        }
        Double asd = bigDecimal.setScale(scale, roundingMode).doubleValue();
        return new DecimalFormat("0.00").format(asd).toString();
    }



    /**
     * 将Object转换成boolean
     * @param object
     * @return
     */
    public static final boolean ObjectToBoolean(Object object){
        return object != null ? Boolean.valueOf(object.toString()).booleanValue() : false;
    }


    /**
     * 将Object转换成int
     * @param object
     * @return
     */
    public static final int ObjectToInt(Object object){
        try{
            if(object instanceof Number){
                return ((Number)object).intValue();
            }
            if(object == null){
                return -1;
            }else{
                return Integer.parseInt(object.toString());
            }
        }catch(NumberFormatException e){
            e.printStackTrace();
            logger.error("ObjectToInt->>转换异常" + e.getMessage());
            return -1;
        }
    }


    /**
     * 将Object转换成short
     * @param object
     * @return
     */
    public static final short ObjectToShort(Object object){
        try{
            if(object instanceof Number){
                return ((Number) object).shortValue();
            }
            if(object == null){
                return -1;
            }else{
                return Short.parseShort(object.toString());
            }
        }catch(NumberFormatException e){
            e.printStackTrace();
            logger.error("ObjectToShort->>转换异常" + e.getMessage());
            return -1;
        }
    }


    /**
     * 将Object转换成double
     * @param object
     * @return
     */
    public static final double ObjectToDouble(Object object){
        try{
            if(object instanceof Number){
                return ((Number)object).doubleValue();
            }
            if(object == null){
                return -1D;
            }else{
                return Double.parseDouble(object.toString());
            }
        }catch(NumberFormatException e){
            e.printStackTrace();
            logger.error("ObjectToDouble->>转换异常" + e.getMessage());
            return -1D;
        }
    }

    /**
     * 将Object转换成long
     * @param object
     * @return
     */
    public static final long ObjectToLong(Object object){
        try{
            if(object instanceof Number){
                return ((Number)object).longValue();
            }
            if(object == null){
                return -1L;
            }else{
                return Long.parseLong(object.toString());
            }
        }catch(NumberFormatException e){
            e.printStackTrace();
            logger.error("ObjectToLong->>转换异常" + e.getMessage());
            return -1L;
        }
    }


    /**
     * 将Object转换成String
     * @param object
     * @param fmt
     * @return
     */
    public static final String ObjectToString(Object object, DecimalFormat fmt) {
        fmt.setDecimalSeparatorAlwaysShown(false);
        if(object instanceof Double){
            return fmt.format(((Double)object).doubleValue());
        }
        if(object instanceof Long){
            return fmt.format(((Long)object).longValue());
        }else{
            return object.toString();
        }
    }


    /**
     * 比较器
     * @param v1
     * @param v2
     * @return
     */
    public static int compareTo(Object v1, Object v2){
    	return convert(v1).compareTo(convert(v2));
    }


    /**
     * 转换器
     * @param object 转换对象
     * @return 返回字符串
     */
    public static String convert(Object object) {
        if(null == object){
            return "0.00";
        }
        if(object instanceof Number){
            return object.toString();
        }
        if(object instanceof Double){
            return object.toString();
        }
        if(object instanceof String){
            return Double.parseDouble(object + "") + "";
        }
            return "0.00";
    }


    /**
     * 通过类型转换成Object
     * @param type
     * @param value
     * @return
     * @throws Exception
     */
    public static Object convertObject(String type, String value) throws Exception{
        try{
            type = type.toLowerCase();
            if("boolean".equals(type)){
                return Boolean.valueOf(value);
            }
            if("byte".equals(type)){
                return Byte.valueOf(value);
            }
            if("short".equals(type)){
                return Short.valueOf(value);
            }
            if("int".equals(type)){
                return Integer.valueOf(value);
            }
            if("long".equals(type)){
                return Long.valueOf(value);
            }
            if("float".equals(type)){
                return Float.valueOf(value);
            }
            if("double".equals(type)){
                return Double.valueOf(value);
            }
            if("string".equals(type)){
                return value;
            }
            if("char".equals(type)){
                return Character.valueOf(value.toCharArray()[0]);
            }else{
                Object objs[] = new String[]{value};
                return Class.forName(type).getConstructor(new Class[] {String.class}).newInstance(objs);
            }
        } catch (Exception e){
               e.printStackTrace();
               logger.error("convertObject->>Exception " + e.getMessage());
               throw new Exception("转换异常");
        }
    }

    /**
     * 获取利用反射获取类里面的值和名称
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
      public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
         Map<String, Object> map = new HashMap<>();
         Class<?> clazz = obj.getClass();
         for (Field field : clazz.getDeclaredFields()) {
                 field.setAccessible(true);
                 String fieldName = field.getName();
                 Object value = field.get(obj);
                 map.put(fieldName, value);
         }
        return map;
      }
}
